/**
 * 定义运行信息模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.sw.operInfo.operInfoManager', ['water.entity.bcSendPlatform',
		                                                                       'water.entity.bcSendPlatformParam',
																				'water.entity.operInfo',
        																        'water.entity.monitorData',
        																		'water.entity.strategy']);
		
	module.controller('operInfoController',function($scope,$bcSendPlatformService,
                                                             $bcSendPlatformParamService,
																$operInfoService,
                                                   			 $monitorDataService,
                                                  			 $strategyService){

        /**
         * 选择的平台地址
         */
        $scope.selectPlatAddr;

        /**
         * 选择的平台Id
         */
        $scope.selectPlatformId;

        /**
         * 选择的平台
         */
        $scope.selectPlatform;

        /**
         * 参数列表
         */
        $scope.parmList;

        /**
		 * 存储策略列表
         */
        $scope.strategyList;

        /**
         * 当前数据
         */
        $scope.curDataList = {};

        /**
		 * 运行信息个数
         */
        $scope.operInfoSize;

        /**
         * 错误运行信息个数
         */
        $scope.errorInfoSize;

        /**
         * 数据存储信息
         */
        $scope.dataStoragerInfo;

        /**
		 * 具体的运行信息
		 */
		$scope.operInfoList = [];
		
		/**
		 * 运行错误信息窗口
		 */
		$scope.operErrorDialog;

        /**
         * 信息详情窗口
         */
        $scope.msgDetailDialog;
		
		/**
		 * 运行信息定时获取器
		 */
		$scope.intervalHandle;

        /**
		 * 当前数据定时获取器
         */
        $scope.getCurDataInterval;

        /**
         * 数据存储信息获取器
         */
        $scope.getDataStoragerInterval;

		/**
		 * 运行错误信息内容
		 */
		$scope.operErrorInfo;

        /**
         * 信息详情
         */
        $scope.msgDetail;


        /**
		 * 轮洵控制，当上一条没有返回的时候，就不进行下一条轮洵
		 */
		$scope.isBack = true;
		
		/**
		 * 自动滚动定时器
		 */
		$scope.autoScrollInterval;

        /**
         * 获取发送目标平台
         */
        $scope.getSendPlatforms = function(){

            $bcSendPlatformService.getModelList().then(function(data){

                var platforms = [];

                for(var i=0;i<data.length;i++){
                    platforms.push({
                        v:data[i].NAME,
                        k:data[i].PLATFORM_ID,
                        data:data[i]
                    });
                }

                $scope.sendPlatforms = platforms;

                if(data.length > 0){
                    $scope.setPlatform(data[0]);
                }
            });
        }

        /**
         * 发送平台选事件
         */
        $scope.onSendPlatformChange = function(key,item){
            $scope.clearOperInfo();
            $scope.operInfoList = [];
            $scope.setPlatform(item.data);
        }

        /**
         * 设置平台
         */
        $scope.setPlatform = function(platform){
            $scope.selectPlatform = platform;
            $scope.selectPlatformId = platform.PLATFORM_ID;

            for(var i=0;i<$scope.sendPlatforms.length;i++){
            	if($scope.sendPlatforms[i].k==$scope.selectPlatformId){
                    $scope.selectPlatAddr =$scope.sendPlatforms[i].data.IP + ":" + $scope.sendPlatforms[i].data.PORT;
				}
			}

            $scope.getMonitorParams($scope.selectPlatformId);

            $strategyService.getListByPlatform($scope.selectPlatformId).then(function(data){
                $scope.strategyList = data;
                $scope.strategyMap = {};
                for(var i=0;i<data.length;i++){
                    var strategy = data[i];
                    $scope.strategyMap[strategy.dataType] = strategy.strategyId;
                }
            });
            /**
             * 切换策略的时候获取当前数据与存储信息
             */
            $scope.getCurData();
            $scope.getDataStoragerInfo();

        }


        /**
         * 获取站点所有的参数
         */
        $scope.getMonitorParams = function(platformId){
            $bcSendPlatformParamService.getListForReport(platformId).then(function(data){

                var params = [];
                for(var i=0; i<data.length; i++){

                    var item = data[i];
                    var descText = "";

                    if(item.ALARM_DOWN && item.ALARM_DOWN){
                        descText = item.ALARM_DOWN + "~" + item.ALARM_UP + "["+item.UNIT+"]";
                    }
                    else if(item.ALARM_DOWN == null && item.ALARM_DOWN == null){
                        descText = "["+item.UNIT+"]";
                    }
                    else if(item.ALARM_DOWN){
                        descText = "<" + item.ALARM_DOWN + "["+item.UNIT+"]";
                    }
                    else if(item.ALARM_UP){
                        descText = ">" + item.ALARM_UP + "["+item.UNIT+"]";
                    }

                    params.push({
                        parmName:item.PARAM_NAME,
                        name:item.PARAM_CODE_BUS,
                        descText:descText
                    });
                }
                $scope.parmList = params;
            });
        }

		
		/**
		 * 开始获取运行信息
		 */
		$scope.startGetOperInfo = function(){
			$scope.autoScrollStart();
            $scope.intervalHandle = setInterval(function(){
                $scope.getOperInfo();
            },'2000');
		}

        /**
         * 获取运行信息
         */
        $scope.getOperInfo = function(){

            if(!$scope.isBack){
                return;
            }

            $scope.isBack = false;

            $operInfoService.getOperInfo($scope.selectPlatAddr).then(function(data){
                $scope.isBack = true;

                if(data!=null){
                    $scope.errorInfoSize = data[0].operErrorInfoSzie;
                    $scope.operInfoSize = data[0].operInfoSize;
                    if(data[0].msg!=null){
                        var log = $scope.processLog(data);
                        $scope.appendLog(log);
					}
                }
            });
        }

        /**
         * 开始获取当前数据
         */
        $scope.startGetCurData = function(){
            $scope.getCurDataInterval = setInterval(function(){
                $scope.getCurData();
            },'10000');
        }

        /**
		 * 获取当前数据
         */
         $scope.getCurData = function(){
             $monitorDataService.getSourceSingleData().then(function(data){
                 var curData = data[0].data;
                 for (var i=0;i<curData.length;i++){
                     for(var j=0;j<$scope.parmList.length;j++){
                         var parm = "MEAS_RE_" + $scope.parmList[j].name;
                         var value = curData[i][parm];
                         var dateTime = curData[i].DATATIME;
                         if(typeof(value) != "undefined"){
                             $scope.curDataList[parm] = value;
                             $scope.curDataList[parm +"_DATATIME"] = dateTime.slice(10);
                         }
                     }
                 }
             });
		 }

        /**
         * 开始获取数据存储信息
         */
        $scope.startGetDataStorager = function(){
            $scope.getDataStoragerInterval = setInterval(function(){
                $scope.getDataStoragerInfo();
            },'60000');
        }

        /**
         * 获取数据存储信息
         */
        $scope.getDataStoragerInfo = function(){
            $operInfoService.getDataStoragerInfo($scope.selectPlatformId).then(function(data){
                $scope.dataStoragerInfo = data[0];
			});
        }



        /**
		 * 查看错误信息点击事件
		 */
		$scope.onErrorInfoClick = function(monitorModel){
			$scope.operErrorDialog.open();
            $operInfoService.getOperErrorInfo().then(function(data){
                $scope.operErrorInfo = data;
            });
		}

        /**
         * 查看信息详情点击时间
         */
        $scope.onMsgDetailClick = function(msg){
            $scope.msgDetailDialog.open();
            $scope.msgDetail = msg;
        }


		/**
		 * 使用正则表达式提取日志中的CN信息
		 */
		$scope.processLog = function(data) {
			if(data.length>=1){
				var msg = data[0].msg;
				var cn = msg.match(/CN=(\S*);PW/);
				if(cn!=null){
					data[0].cn=cn[1];
					return data[0];
				}else{
					return data[0];
				}
			}else{
				var msg = data.msg;
				var cn = msg.match(/CN=(\S*);PW/);
				if(cn!=null){
					data.cn=cn[1];
					return data;
				}else{
					return data;
				}
			}
		}

		/**
		 * 把日志信息添加到界面上
		 */
		$scope.appendLog = function(log){
			if(!log){
				return;
			}
			
			/**
			 * 如果不是自动滚动，就不增加
			 */
			if($scope.autoScrollInterval == null){
				return;
			}
			
			if($scope.operInfoList.length > 100){
				$scope.operInfoList = [];
			}
			$scope.operInfoList.push(log);
		}
		
		$scope.$on('$destroy', function(){
			clearInterval($scope.intervalHandle);
            clearInterval($scope.getCurDataInterval);
		});
		
		/**
		 * 启动或者停止滚动
		 */
		$scope.switchAutoScroll = function(){
			if($scope.autoScrollInterval != null){
				$scope.autoScrollStop();
			}
			else{
				$scope.autoScrollStart();
			}
		}
	
		/**
		 * 自动滚动到底部
		 */
		$scope.autoScrollStart = function(){
			
			/**
			 * 如果当前已有定时器，先清除
			 */
			$scope.autoScrollStop();

			$scope.autoScrollInterval = setInterval(function(){
				var log = $("#logDiv")[0];
				var table = $("#logTable")[0];

				if(log == null || table == null){
					return;
				}

				var height = table.scrollHeight;
				if(height!=0){
					log.scrollTop=log.scrollHeight;
				}
			},500);
			
		}
		
		/**
		 * 停止自动滚动到底部
		 */
		$scope.autoScrollStop = function(){
			
			if($scope.autoScrollInterval != null){
				clearInterval($scope.autoScrollInterval);
			}
			$scope.autoScrollInterval = null;
		}

        /**
		 * 清空运行信息
         */
        $scope.clearOperInfo = function(){
            $operInfoService.clearOperInfo().then(function (value) {

			});
		}

        $scope.getSendPlatforms();
        $scope.startGetCurData();
        $scope.clearOperInfo();
        $scope.startGetOperInfo();
        $scope.startGetDataStorager();

    });
	
}(window, angular));


